package com.wejoy.video.middle.station.data.util.baiduevs;/**
 * @Author 张文杰
 * @PackageName video-middle-station
 * @Package com.wejoy.video.middle.station.data.util.baiduevs
 * @Date 2022/11/11 5:17 PM
 * @Version 1.0
 */

import com.baidubce.auth.BceV1Signer;
import com.baidubce.auth.DefaultBceCredentials;
import com.baidubce.http.HttpMethodName;
import com.baidubce.internal.InternalRequest;
import com.wejoy.video.middle.station.data.util.BeanUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;

/**
 * @Description :百度EVS请求工具类
 * @BelongsProject: video-middle-station
 * @BelongsPackage: com.wejoy.video.middle.station.data.util.baiduevs
 * @Author: zhangwenjie
 * @CreateTime: 2022-11-11  17:17
 * @Description: TODO
 * @Version: 1.0
 */
@Component
public class BaiduEVSRequestUtil {

    @Value("${baidu.evs.ak}")
    private String ACCESS_KEY_ID;
    @Value("${baidu.evs.sk}")
    private String SECRET_ACCESS_KEY;

    /**
     * 百度EVS统一请求方法入口
     * @param headers 请求头 @see BaiduRequestSignatureHaders
     * @param params 请求参数
     * @param url   请求URL @see  BaiduEVSConstant
     * @param httpMethod  http请求方法 @see HttpMethodName
     * @return @see HttpResult
     */
    public HttpResult evsRequest(BaiduRequestSignatureHaders headers, Map<String, String> params, String url, HttpMethodName httpMethod){
        InternalRequest signature = signature(BeanUtil.beanToMap(headers), params, url, httpMethod);
        HttpResult httpResult = null;
        try {
            httpResult = new EvsApiService().doGet(url, signature.getHeaders(), params);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  httpResult;
    }


    /**
     *
     * @param headers 需要签名的请求头部信息
     * @param params    需要签名的参数
     * @param url   需要签名的URL
     * @param httpMethod 访问类型 @see com.baidubce.http.HttpMethodName
     * @return
     */
    private InternalRequest signature(Map<String,String> headers,Map<String, String> params,String url,HttpMethodName httpMethod) {
        BceV1Signer bceV1Signer = new BceV1Signer();
        URI uri = null;
        try {
            uri = new URI(url);
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        InternalRequest request = new InternalRequest(httpMethod,uri);
        //设置需要签名的请求头和参数
        request.setHeaders(headers);
        request.setParameters(params);
        bceV1Signer.sign(request,  new DefaultBceCredentials(ACCESS_KEY_ID, SECRET_ACCESS_KEY));
        return  request;
    }

//    public static void main(String[] args) {
//        Map<String,String> paams = Maps.newHashMap();
//        paams.put("pageNo","1");
//        paams.put("pageSize","10");
//        paams.put("type","001");
//        String url = BaiduEVSConstant.EVS_BASE_URL+ BaiduEVSConstant.EVS_APP;
//        System.out.println("result:====="+ evsRequest(new BaiduRequestSignatureHaders(
//                BaiduEVSConstant.EVS_BASE_HOST),paams,url,HttpMethodName.GET).toString());
//    }

}
